package edu.localisationPhone;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import edu.localisationPhone.R.layout;

public class SauveteurActivity extends MapActivity implements LocationListener {
	
	MapView mv;
	MapController mc;
	
	double latitude, longitude;
	
	List<Overlay> mapOverlays;
	Drawable drawable;
	ElementOverlay itemizedOverlay;
	
	GestionSMS g;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		
		mv = (MapView)findViewById(R.id.mapview);
        mv.setBuiltInZoomControls(true);        
        
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 100.0f, this);
        
        mapOverlays = mv.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.map_pin_red);
        itemizedOverlay = new ElementOverlay(drawable);
        
        IntentFilter f = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        g = new GestionSMS(this);
        registerReceiver(g, f);
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLocationChanged(Location location) {
		if (location != null) {
			this.latitude = location.getLatitude();
			this.longitude = location.getLongitude();
			this.mc = mv.getController();
			this.mc.setZoom(14);
			GeoPoint point = new GeoPoint((int)(latitude*1e6), (int)(longitude*1e6));
			this.mc.setCenter(point);
			OverlayItem marker = new OverlayItem(point, "Position actuelle", "");
			this.itemizedOverlay.ajouterOverlay(marker);
			this.mapOverlays.add(itemizedOverlay);
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
		if (item.getItemId() == R.id.localiser) {
	        /* Display a text entry dialog */
	        showDialog(1);			
	        return true;
		} else {
			Toast t2 = Toast.makeText(getApplicationContext(), "coucou2", Toast.LENGTH_LONG);
	    	t2.show();
			return super.onOptionsItemSelected(item);
		}
	}
	
	protected Dialog onCreateDialog(int id) {
        // This example shows how to add a custom layout to an AlertDialog
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.send_sms, null);
        return new AlertDialog.Builder(SauveteurActivity.this)
            .setTitle(R.string.localiser)
            .setView(textEntryView)
            .setPositiveButton(R.string.boutonSend, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	g.sendSMS(((EditText)textEntryView.findViewById(R.id.nbPhoneEdit)).getText().toString(), "wru");
                }
            })
            .setNegativeButton(R.string.boutonCancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	dialog.dismiss();
                }
            })
            .create();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
}
